package demo;
import java.util.ArrayList;
import java.util.List;
public class Solution {
    //求一颗树的高度
    public int heightOf(TreeNode root){
        if(root==null){
            return 0;
        }
        return Integer.max(heightOf(root.left),heightOf(root.right))+1;
    }
    //遍历每一个结点,将它们放到顺序表
    public List<TreeNode> search(TreeNode root){
        List<TreeNode> list = new ArrayList<>();
        if(root==null){
            return list;
        }
        list.add(root);
        list.addAll(search(root.left));
        list.addAll(search(root.right));
        return list;
    }
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        //遍历每一个结点
        List<TreeNode> list= search(root);
        for(TreeNode ans:list){
            //
            int leftResult=heightOf(ans.left);
            int rightResult=heightOf(ans.right);
            int diff=leftResult-rightResult;
            if(diff<=-2||diff>=2){
                return false;
            }
        }
        return true;
    }
}
